package com.example.springboot.controller.wheather;

import com.example.springboot.utils.GetIPUtils;
import com.example.springboot.utils.HttpRequest;
import com.example.springboot.utils.IpUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/1 18:53
 */
@RestController
@RequestMapping("/weather")
@Slf4j
public class WeatherController {
//    @GetMapping("/get")
//    public Object weather(HttpServletRequest request) throws DbMakerConfigException, IOException {
//        // 通过万维网访问真实的ip
//        String ip = GetIPUtils.getIp();
//        // 拿到假的ip
//        String ipAddr = IpUtil.getIpAddr(request);
//        if (ip == null) {
//            return "ip为空";
//        }
//        String cityInfo = IpUtil.getAddress(ip);//国家|区域|省份|城市|ISP
//        if (cityInfo == null) {
//            return "城市获取失败";
//        }
//        String[] split = cityInfo.split("\\|");
//        String city = "";
//        for (String aSplit : split) if (aSplit.contains("市")) city = aSplit;//拿取市级名称
//
//        if (city.equals("")) {
//            return "城市获取失败";
//        }
//        // 访问气象台的数据
//        String weatherInformation = HttpRequest.sendGet("http://portalweather.comsys.net.cn/weather03/api/weatherService/getDailyWeather", "cityName=" + city);//调用天气接口
//        if (weatherInformation == null) {
//            return "获取失败";
//        }
//        ObjectMapper objectMapper = new ObjectMapper();
//        Object o = objectMapper.readValue(weatherInformation, Object.class);
//        return o;
//    }
}
